package jacidco;

import java.awt.Color;

/**
 *
 * @author David Cuellar
 */
public class CreaNivelFrame extends javax.swing.JFrame
{
    

    /** Al iniciar el Frame, carga el siguiente codigo valido a registrar
      * @see BaseDatos  
     */
    public CreaNivelFrame()
    {
        initComponents();
        
        Main.bd.conectar();
        
        txtCodNivel.setText(""+(Main.bd.nextCode("cod_nivel", "nivel") + 1));
        
        Main.bd.cerrarConexion();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTitulo = new javax.swing.JLabel();
        lblTitulo4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        lblCodNivel = new javax.swing.JLabel();
        txtCodNivel = new javax.swing.JTextField();
        lblNombreNivel = new javax.swing.JLabel();
        txtNombreNivel = new javax.swing.JTextField();
        lblDescripcionNivel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaDescripcionNivel = new javax.swing.JTextArea();
        btnCancelar = new javax.swing.JButton();
        btmCrear = new javax.swing.JButton();
        btnNuevo = new javax.swing.JButton();
        lblMarcareg = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        lblTitulo.setFont(new java.awt.Font("Times New Roman", 3, 18));
        lblTitulo.setText("Comunidad Dinamica en Java");
        lblTitulo.setName("lblTitulo"); // NOI18N

        lblTitulo4.setFont(new java.awt.Font("Times New Roman", 3, 36));
        lblTitulo4.setText("Niveles de Estudio");
        lblTitulo4.setName("lblTitulo4"); // NOI18N

        jLabel1.setBackground(new java.awt.Color(255, 51, 51));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jacidco/resources/Logo_Jacidco_mini.png"))); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setName("jPanel1"); // NOI18N

        lblCodNivel.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblCodNivel.setText("Codigo Nivel :");
        lblCodNivel.setName("lblCodNivel"); // NOI18N

        txtCodNivel.setEditable(false);
        txtCodNivel.setName("txtCodNivel"); // NOI18N

        lblNombreNivel.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblNombreNivel.setText("Nombre :");
        lblNombreNivel.setName("lblNombreNivel"); // NOI18N

        txtNombreNivel.setName("txtNombreNivel"); // NOI18N

        lblDescripcionNivel.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblDescripcionNivel.setText("Descripcion :");
        lblDescripcionNivel.setName("lblDescripcionNivel"); // NOI18N

        jScrollPane1.setToolTipText("");
        jScrollPane1.setAutoscrolls(true);
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txaDescripcionNivel.setColumns(20);
        txaDescripcionNivel.setFont(new java.awt.Font("Tahoma", 0, 11));
        txaDescripcionNivel.setLineWrap(true);
        txaDescripcionNivel.setRows(5);
        txaDescripcionNivel.setName("txaDescripcionNivel"); // NOI18N
        jScrollPane1.setViewportView(txaDescripcionNivel);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(lblDescripcionNivel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblNombreNivel)
                            .addComponent(lblCodNivel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtCodNivel)
                            .addComponent(txtNombreNivel, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE))))
                .addGap(21, 21, 21))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCodNivel)
                    .addComponent(txtCodNivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNombreNivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNombreNivel))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDescripcionNivel)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        btnCancelar.setFont(new java.awt.Font("Tahoma", 3, 11));
        btnCancelar.setText("Cancelar");
        btnCancelar.setName("btnCancelar"); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btmCrear.setFont(new java.awt.Font("Tahoma", 3, 11));
        btmCrear.setText("Crear");
        btmCrear.setName("btmCrear"); // NOI18N
        btmCrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btmCrearActionPerformed(evt);
            }
        });

        btnNuevo.setFont(new java.awt.Font("Tahoma", 3, 11));
        btnNuevo.setText("Nuevo");
        btnNuevo.setName("btnNuevo"); // NOI18N
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        lblMarcareg.setFont(new java.awt.Font("Times New Roman", 0, 11));
        lblMarcareg.setText("Copyright 2011. DCM. All Rights Reserved");
        lblMarcareg.setName("lblMarcareg"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lblTitulo, 0, 0, Short.MAX_VALUE)
                            .addComponent(lblTitulo4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(157, 157, 157)
                        .addComponent(btnNuevo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btmCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblMarcareg))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(39, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblTitulo)
                        .addGap(67, 67, 67)
                        .addComponent(lblTitulo4)))
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btmCrear)
                    .addComponent(btnNuevo))
                .addGap(18, 18, 18)
                .addComponent(lblMarcareg)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        
        nuevoNivel();
        
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        
        Main.goToMenuPrincipal();
        dispose();
        
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btmCrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btmCrearActionPerformed
        
        capturaDatos();
        guardaDatos();

    }//GEN-LAST:event_btmCrearActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btmCrear;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCodNivel;
    private javax.swing.JLabel lblDescripcionNivel;
    private javax.swing.JLabel lblMarcareg;
    private javax.swing.JLabel lblNombreNivel;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JLabel lblTitulo4;
    private javax.swing.JTextArea txaDescripcionNivel;
    private javax.swing.JTextField txtCodNivel;
    private javax.swing.JTextField txtNombreNivel;
    // End of variables declaration//GEN-END:variables
   
    /**
     * Captura los datos ingresados en los JTaxtField y los almacena en el objeto
     * nivel
     */
    public void capturaDatos()
    {
        Main.nivel.setCod(Byte.parseByte(txtCodNivel.getText()));
        Main.nivel.setNombre(txtNombreNivel.getText());
        Main.nivel.setDescripcion(txaDescripcionNivel.getText());
    }
    
    /**
     * Realiza la validacion de campos vacios y envias los datos almacenados en el objeto nivel
     * al metodo insertNivel del objeto bd.
     * @see Nivel
     * @see BaseDatos
     */
    public void guardaDatos()
    {
        if(!Main.nivel.getNombre().equals("") && !Main.nivel.getDescripcion().equals(""))
        {
            Main.bd.conectar();
            if(Main.bd.insertNivel(Main.nivel.getCod(), Main.nivel.getNombre(), Main.nivel.getDescripcion()));
            {
                Main.mensaje.datosGuardaosOK("Nivel Creado");
            }
            Main.bd.cerrarConexion();

            txtNombreNivel.setEditable(false);
            txaDescripcionNivel.setEditable(false);
            txaDescripcionNivel.setBackground(Color.lightGray);
        }
        else
        {
            Main.mensaje.noCamposVacios();
        }
    }
    
    /**
     * Limpia  y habilita los campos para registrar un nuevo nivel
     */
    public void nuevoNivel()
    {
        txtCodNivel.setText("");
        txtNombreNivel.setText("");
        txaDescripcionNivel.setText("");
        
        Main.bd.conectar();
        
        txtCodNivel.setText(""+(Main.bd.nextCode("cod_nivel", "nivel") + 1));
                
        Main.bd.cerrarConexion();
        
        txtNombreNivel.setEditable(true);
        txaDescripcionNivel.setEditable(true);
        txaDescripcionNivel.setBackground(Color.white);
    }
}
